package com.yanqu.road.server.cmd.activity.treasure;

import com.yanqu.road.pb.treasureactivity.TreasureActivityProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.treasure.TreasureActivityModule;
import com.yanqu.road.entity.activity.treasure.TreasureResult;
import com.yanqu.road.server.manger.activity.treasure.TreasureActivityMgr;
import com.yanqu.road.server.pb.TreasureActivityPb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = Protocol.S_ACTIVITY_TREASURE_ROUND_REWARD, desc = "灵狐寻宝领取圈数奖励")
public class TreasureGetRoundRewardCmd extends AbstractCommand {

    int activityId;
    TreasureResult result;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        TreasureActivityProto.TreasureGetRoundRewardReqMsg reqMsg = TreasureActivityProto.TreasureGetRoundRewardReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {

        result = player.getModule(TreasureActivityModule.class).getRoundReward(activityId);
    }

    @Override
    public void sendData(GamePlayer player) {
        TreasureActivityProto.TreasureGetRoundRewardRespMsg.Builder resp = TreasureActivityProto.TreasureGetRoundRewardRespMsg.newBuilder();
        resp.setRet(result.getRet());
        if(result.getRet()==0){
            resp.setTokenRewards(result.getRewards());
            if(result.getUserTreasureActivityData()!=null){
                resp.setUserTreasureData(TreasureActivityPb.parseUserDataTempBuilder(result.getUserTreasureActivityData(), TreasureActivityMgr.getUserDisperseGoldTimes(result.getUserTreasureActivityData()), TreasureActivityMgr.getPlaceInfo(activityId, result.getUserTreasureActivityData().getRouteIndex())));
            }
        }
        player.sendPacket(ClientProtocol.U_ACTIVITY_TREASURE_ROUND_REWORD,resp);
    }
}
